package com.ruoyi.web.controller.sujian;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SjWaterMeter;
import com.ruoyi.system.domain.SjWaterMeter;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.service.ISysFloorService;
import com.ruoyi.system.service.IWaterMeterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 水费抄表Controller
 *
 */
@Controller
@RequestMapping("/sujian/waterMeter")
public class WaterMeterController extends BaseController
{
    private String prefix = "propertys/sujian/waterRateManagement/waterMeter";

    @Autowired
    private IWaterMeterService waterMeterService;

    @Autowired
    private ISysFloorService floorInfoService;

    @GetMapping()
    public String waterMeter(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/main";
    }

    /**
     * 抄表列表查询
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjWaterMeter sjWaterMeter)
    {
        startPage();
        List<SjWaterMeter> sjWaterMeterList = waterMeterService.getWaterMeterList(sjWaterMeter);
        return getDataTable(sjWaterMeterList);
    }

    /**
     * 状态修改
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SjWaterMeter sjWaterMeter){
        try {
            waterMeterService.changeWaterMeterStatus(sjWaterMeter);
        }catch (Exception e){
            return AjaxResult.error("状态修改失败："+e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 抄表信息添加
     */
    @GetMapping("/add")
    public String toAdd(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/add";
    }

    /**
     * 抄表信息添加保存
     */
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(SjWaterMeter sjWaterMeter){
        try {
            SjWaterMeter sjWm = waterMeterService.selectWaterMeterByFloorAndEtldt(sjWaterMeter);
            if(sjWm!=null&&!sjWm.getAmmeterId().equals(sjWaterMeter.getAmmeterId())){
                return AjaxResult.error("该月份下已有抄表记录");
            }
            int i = waterMeterService.insertWaterMeter(sjWaterMeter);
            if(i>0){
                return AjaxResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("新增失败："+e.getMessage());
        }
        return AjaxResult.error("保存失败");
    }

    /**
     * 抄表信息修改
     */
    @GetMapping("/edit/{ammeterId}")
    public String toEdit(@PathVariable("ammeterId") String ammeterId, ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        SjWaterMeter sjWaterMeter = waterMeterService.getWaterMeterById(ammeterId);
        modelMap.put("sjWaterMeter",sjWaterMeter);
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/edit";
    }

    /**
     * 抄表信息修改保存
     */
    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(SjWaterMeter sjWaterMeter){
        try {
            SjWaterMeter sjWm = waterMeterService.selectWaterMeterByFloorAndEtldt(sjWaterMeter);
            if(sjWm!=null&&!sjWm.getAmmeterId().equals(sjWaterMeter.getAmmeterId())){
                return AjaxResult.error("该月份下已有抄表记录");
            }
            int i = waterMeterService.updateWaterMeter(sjWaterMeter);
            if(i>0){
                return AjaxResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("修改失败："+e.getMessage());
        }
        return AjaxResult.error("保存失败");
    }

    /**
     * 抄表信息删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        try {
            int i = waterMeterService.removeWaterMeter(ids);
            if(i>0){
                return AjaxResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("删除失败："+e.getMessage());
        }
        return AjaxResult.error("删除失败");
    }

    //导入抄表数据
    @PostMapping("/importExcel")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SjWaterMeter> util = new ExcelUtil<>(SjWaterMeter.class);
        List<SjWaterMeter> lists = util.importExcel(file.getInputStream());
        //校验数据
        String message =waterMeterService.importWaterMeter(lists,updateSupport);
        return AjaxResult.success(message);
    }


    //导入抄表数据模板
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<SjWaterMeter> util = new ExcelUtil<SjWaterMeter>(SjWaterMeter.class);
        return util.importTemplateExcel("抄表信息数据");
    }
}
